package com.edt.ichannel.api.service.impl;

import cc.edt.iceutils2.string.IceStringUtils;
import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.entiy.local.*;
import com.edt.ichannel.api.service.*;
import com.edt.ichannel.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@Slf4j
@Service
public class PutOutCommissionServiceImpl implements PutoutCommissionService {

    @Resource
    private StaffService staffService;
    @Resource
    private ExpansionService expansionService;
    @Resource
    private MechanismsService mechanismsService;
    @Resource
    private AccountLogService accountLogService;
    @Resource
    private OrderChannelService orderChannelService;
    @Resource
    private OrderCreditMonthService orderCreditMonthService;

    @Resource
    private CommissionSuperaddService commissionSuperaddService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionCk(OrderCreditCK orderCreditCK) {
        if(orderCreditCK != null){
            orderChannelService.setExpansionTypeAndLevl(orderCreditCK);
            if (CommonConstant.EXPANSION_LEVEL_ZERO == orderCreditCK.getChannelLevel()) {
                putoutCommissionMoneyZero(orderCreditCK);
            } else {
                switch (orderCreditCK.getChannelType()) {
                    case CommonConstant.STAFF_TYPE_CB:
                        putoutCommissionMoneyCb(orderCreditCK);
                        break;
                    case CommonConstant.STAFF_TYPE_QIANY:
                        putoutCommissionMoneyQianY(orderCreditCK);
                        break;
                    case CommonConstant.STAFF_TYPE_QUANY:
                        putoutCommissionMoneyQuanY(orderCreditCK);
                        break;
                    default:
                        putoutCommissionMoneyZy(orderCreditCK);
                        break;
                }
            }
        }

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionKd(OrderCreditKd orderCreditKd) {
        if(orderCreditKd != null){
            orderChannelService.setExpansionTypeAndLevl(orderCreditKd);
            if (CommonConstant.EXPANSION_LEVEL_ZERO == orderCreditKd.getChannelLevel()) {
                putoutCommissionMoneyZero(orderCreditKd);
            } else {
                switch (orderCreditKd.getChannelType()) {
                    case CommonConstant.STAFF_TYPE_CB:
                        putoutCommissionMoneyCb(orderCreditKd);
                        break;
                    case CommonConstant.STAFF_TYPE_QIANY:
                        putoutCommissionMoneyQianY(orderCreditKd);
                        break;
                    case CommonConstant.STAFF_TYPE_QUANY:
                        putoutCommissionMoneyQuanY(orderCreditKd);
                        break;
                    default:
                        putoutCommissionMoneyZy(orderCreditKd);
                        break;
                }
            }
        }
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMonth(OrderCreditMonth orderCreditMonth) {
        if(orderCreditMonth != null){
            orderChannelService.setExpansionTypeAndLevl(orderCreditMonth);
            if (CommonConstant.EXPANSION_LEVEL_ZERO == orderCreditMonth.getChannelLevel()) {
                putoutCommissionMoneyZero(orderCreditMonth);
            } else {
                switch (orderCreditMonth.getChannelType()) {
                    case CommonConstant.STAFF_TYPE_CB:
                        putoutCommissionMoneyCb(orderCreditMonth);
                        break;
                    case CommonConstant.STAFF_TYPE_QIANY:
                        putoutCommissionMoneyQianY(orderCreditMonth);
                        break;
                    case CommonConstant.STAFF_TYPE_QUANY:
                        putoutCommissionMoneyQuanY(orderCreditMonth);
                        break;
                    default:
                        putoutCommissionMoneyZy(orderCreditMonth);
                        break;
                }
            }
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionBf(OrderCreditBf orderCreditBf) {
        if(orderCreditBf != null){
            if(IceStringUtils.isBlank(orderCreditBf.getChannelId())){
                orderChannelService.setExpansionTypeAndLevl(orderCreditBf);
            }
            if (CommonConstant.EXPANSION_LEVEL_ZERO == orderCreditBf.getChannelLevel()) {
                putoutCommissionMoneybfZero(orderCreditBf);
            } else {
                putoutCommissionMoneyBf(orderCreditBf);
            }
        }
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneyBf(OrderCreditBf orderCreditBf) {
        if(orderCreditBf != null){
            Expansion expansion = expansionService.getExpansionById(orderCreditBf.getChannelId());
            if (expansion != null) {
                expansionService.addExpansionCountMoney(orderCreditBf.getCommissionMoney(), expansion.getId());
                //更新m_accountlog
                saveLogBf(orderCreditBf,expansion.getStaffId());
            }
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putOutCommissionSuperadd(CommissionSuperadd commissionSuperadd) {
        if(commissionSuperadd != null){
            if (CommonConstant.EXPANSION_LEVEL_ZERO == commissionSuperadd.getChannelLevel()) {
                Staff staff = staffService.getStaffById(commissionSuperadd.getChannelId());
                if (staff != null) {
                    staffService.addStaffAccountMoney(commissionSuperadd.getCommissionMoney(), staff.getId());
                    staffService.addStaffCommissionMoney(commissionSuperadd.getCommissionMoney(), staff.getId());
                    saveLogSuperadd(commissionSuperadd, staff.getId());
                }
            } else {
                Expansion expansion = expansionService.getExpansionById(commissionSuperadd.getChannelId());
                expansionService.addExpansionCountMoney(commissionSuperadd.getCommissionMoney(), expansion.getId());
                saveLogSuperadd(commissionSuperadd, null);
            }
            commissionSuperaddService.updateCommissionSuperaddToPutoutOver(commissionSuperadd.getId());
        }
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneybfZero(OrderCreditBf orderCreditBf) {
        if(orderCreditBf != null){
            Staff staff = staffService.getStaffById(orderCreditBf.getChannelId());
            if (staff != null) {
                staffService.addStaffAccountMoney(orderCreditBf.getCommissionMoney(), staff.getId());
                staffService.addStaffCommissionMoney(orderCreditBf.getCommissionMoney(), staff.getId());
                // 更新m_accountlog
                saveLogBf(orderCreditBf,staff.getId());
            }
        }
    }



    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneyZero(OrderCreditCK orderCreditCK) {
        if(orderCreditCK != null){
            Staff staff = staffService.getStaffById(orderCreditCK.getChannelId());
            if (staff != null) {
                staffService.addStaffAccountMoney(orderCreditCK.getCommissionMoney(), staff.getId());
                staffService.addStaffCommissionMoney(orderCreditCK.getCommissionMoney(), staff.getId());
                saveLogCk(orderCreditCK, null, null, orderCreditCK.getCommissionMoney(), CommonConstant.EXPANSION_LEVEL_ZERO, staff.getId());
            }
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneyZero(OrderCreditMonth orderCreditMonth) {
        if(orderCreditMonth != null){
            Staff staff = staffService.getStaffById(orderCreditMonth.getChannelId());
            if (staff != null) {
                staffService.addStaffAccountMoney(orderCreditMonth.getCommissionMoney(), staff.getId());
                staffService.addStaffCommissionMoney(orderCreditMonth.getCommissionMoney(), staff.getId());
                saveLogMonth(orderCreditMonth, null, null, orderCreditMonth.getCommissionMoney(), CommonConstant.EXPANSION_LEVEL_ZERO, staff.getId());
            }
        }
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneyZero(OrderCreditKd orderCreditKd) {
        if(orderCreditKd != null){
            Staff staff = staffService.getStaffById(orderCreditKd.getChannelId());
            if (staff != null) {
                staffService.addStaffAccountMoney(orderCreditKd.getCommissionMoney(), staff.getId());
                staffService.addStaffCommissionMoney(orderCreditKd.getCommissionMoney(), staff.getId());
                saveLogKd(orderCreditKd, null, null, orderCreditKd.getCommissionMoney(), CommonConstant.EXPANSION_LEVEL_ZERO, staff.getId());
            }
        }

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneyZy(OrderCreditCK orderCreditCK) {
        if (orderCreditCK != null) {
            Expansion expansion = expansionService.getExpansionById(orderCreditCK.getChannelId());
            //自有人员，2级给1级返佣 有发展奖
            if (expansion.getExpansionLevel() == CommonConstant.EXPANSION_LEVEL_SECOND) {

                BigDecimal com = new BigDecimal(orderCreditCK.getCommissionMoney());
                BigDecimal firstRat = new BigDecimal(expansion.getToFirstRate());
                BigDecimal firstCommissionMoney = com.multiply(firstRat).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal secondCommissionMoney = com.subtract(firstCommissionMoney);
                expansionService.addExpansionCountMoney(secondCommissionMoney.doubleValue(), expansion.getId());
                saveLogCk(orderCreditCK, expansion, null, secondCommissionMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_SECOND, null);
                if(expansion.getToFirstRate() != 0){
                    Expansion parentExpansion = expansionService.getExpansionById(expansion.getParentId());
                    expansionService.addExpansionCountMoney(firstCommissionMoney.doubleValue(), parentExpansion.getId());
                    saveLogCk(orderCreditCK, parentExpansion, expansion, firstCommissionMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_FIRST, null);
                }

            } else if (expansion.getExpansionLevel() == CommonConstant.EXPANSION_LEVEL_FIRST) {
                expansionService.addExpansionCountMoney(orderCreditCK.getCommissionMoney(), expansion.getId());
                saveLogCk(orderCreditCK, expansion, null, orderCreditCK.getCommissionMoney(), CommonConstant.EXPANSION_LEVEL_FIRST, null);
            }
        }
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneyZy(OrderCreditKd orderCreditKd) {
        if (orderCreditKd != null) {
            Expansion expansion = expansionService.getExpansionById(orderCreditKd.getChannelId());
            //自有人员，2级给1级返佣 有发展奖
            if (expansion.getExpansionLevel() == CommonConstant.EXPANSION_LEVEL_SECOND) {

                BigDecimal com = new BigDecimal(orderCreditKd.getCommissionMoney());
                BigDecimal firstRat = new BigDecimal(expansion.getToFirstRate());
                BigDecimal firstCommissionMoney = com.multiply(firstRat).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal secondCommissionMoney = com.subtract(firstCommissionMoney);
                expansionService.addExpansionCountMoney(secondCommissionMoney.doubleValue(), expansion.getId());
                saveLogKd(orderCreditKd, expansion, null, secondCommissionMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_SECOND, null);
                if(expansion.getToFirstRate() != 0){
                    Expansion parentExpansion = expansionService.getExpansionById(expansion.getParentId());
                    expansionService.addExpansionCountMoney(firstCommissionMoney.doubleValue(), parentExpansion.getId());
                    saveLogKd(orderCreditKd, parentExpansion, expansion, firstCommissionMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_FIRST, null);
                }

            } else if (expansion.getExpansionLevel() == CommonConstant.EXPANSION_LEVEL_FIRST) {
                expansionService.addExpansionCountMoney(orderCreditKd.getCommissionMoney(), expansion.getId());
                saveLogKd(orderCreditKd, expansion, null, orderCreditKd.getCommissionMoney(), CommonConstant.EXPANSION_LEVEL_FIRST, null);
            }
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneyZy(OrderCreditMonth orderCreditMonth) {
        if (orderCreditMonth != null) {
            Expansion expansion = expansionService.getExpansionById(orderCreditMonth.getChannelId());
            //自有人员，2级给1级返佣 有发展奖
            if (expansion.getExpansionLevel() == CommonConstant.EXPANSION_LEVEL_SECOND) {

                BigDecimal com = new BigDecimal(orderCreditMonth.getCommissionMoney());
                BigDecimal firstRat = new BigDecimal(expansion.getToFirstRate());
                BigDecimal firstCommissionMoney = com.multiply(firstRat).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal secondCommissionMoney = com.subtract(firstCommissionMoney);
                expansionService.addExpansionCountMoney(secondCommissionMoney.doubleValue(), expansion.getId());
                saveLogMonth(orderCreditMonth, expansion, null, secondCommissionMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_SECOND, null);
                if(expansion.getToFirstRate() != 0){
                    Expansion parentExpansion = expansionService.getExpansionById(expansion.getParentId());
                    expansionService.addExpansionCountMoney(firstCommissionMoney.doubleValue(), parentExpansion.getId());
                    saveLogMonth(orderCreditMonth, parentExpansion, expansion, firstCommissionMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_FIRST, null);
                }

            } else if (expansion.getExpansionLevel() == CommonConstant.EXPANSION_LEVEL_FIRST) {
                expansionService.addExpansionCountMoney(orderCreditMonth.getCommissionMoney(), expansion.getId());
                saveLogMonth(orderCreditMonth, expansion, null, orderCreditMonth.getCommissionMoney(), CommonConstant.EXPANSION_LEVEL_FIRST, null);
            }
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneyCb(OrderCreditCK orderCreditCK) {
        if (orderCreditCK != null) {
            Expansion expansion = expansionService.getExpansionById(orderCreditCK.getChannelId());
            //自有人员，2级给1级返佣 有发展奖
            if (expansion.getExpansionLevel() == CommonConstant.EXPANSION_LEVEL_SECOND) {
                BigDecimal com = new BigDecimal(orderCreditCK.getCommissionMoney());
                BigDecimal fitstRat = new BigDecimal(expansion.getToFirstRate());
                BigDecimal zeroRat = new BigDecimal(expansion.getToZeroRate());
                BigDecimal zeroAddMoney = com.multiply(zeroRat).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal firstAddMoney = com.multiply(fitstRat).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal addMoney = com.subtract(zeroAddMoney).subtract(firstAddMoney);
                expansionService.addExpansionCountMoney(addMoney.doubleValue(), expansion.getId());
                saveLogCk(orderCreditCK, expansion, null, addMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_SECOND, null);
                if(expansion.getToFirstRate() != 0){
                    Expansion parentExpansion = expansionService.getExpansionById(expansion.getParentId());
                    expansionService.addExpansionCountMoney(firstAddMoney.doubleValue(), expansion.getParentId());
                    saveLogCk(orderCreditCK, parentExpansion, expansion, firstAddMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_FIRST, null);
                }

                if(expansion.getToZeroRate() != 0){
                    staffService.addStaffAccountMoney(zeroAddMoney.doubleValue(), expansion.getStaffId());
                    staffService.addStaffCommissionMoney(zeroAddMoney.doubleValue(), expansion.getStaffId());
                    saveLogCk(orderCreditCK, null, expansion, zeroAddMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_ZERO, expansion.getStaffId());
                }
            } else if (expansion.getExpansionLevel() == CommonConstant.EXPANSION_LEVEL_FIRST) {
                BigDecimal com = new BigDecimal(orderCreditCK.getCommissionMoney());
                BigDecimal zeroRat = new BigDecimal(expansion.getToZeroRate());
                BigDecimal zeroAddMoney = com.multiply(zeroRat).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal addMoney = com.subtract(zeroAddMoney);
                expansionService.addExpansionCountMoney(addMoney.doubleValue(), expansion.getId());
                saveLogCk(orderCreditCK, expansion, null, addMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_FIRST, null);
                if(expansion.getToZeroRate() != 0){
                    staffService.addStaffAccountMoney(zeroAddMoney.doubleValue(), expansion.getStaffId());
                    staffService.addStaffCommissionMoney(zeroAddMoney.doubleValue(), expansion.getStaffId());
                    saveLogCk(orderCreditCK, null, expansion, zeroAddMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_ZERO, expansion.getStaffId());
                }

            }
        }
    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneyCb(OrderCreditKd orderCreditKd) {
        if (orderCreditKd != null) {
            Expansion expansion = expansionService.getExpansionById(orderCreditKd.getChannelId());
            //自有人员，2级给1级返佣 有发展奖
            if (expansion.getExpansionLevel() == CommonConstant.EXPANSION_LEVEL_SECOND) {
                BigDecimal com = new BigDecimal(orderCreditKd.getCommissionMoney());
                BigDecimal fitstRat = new BigDecimal(expansion.getToFirstRate());
                BigDecimal zeroRat = new BigDecimal(expansion.getToZeroRate());
                BigDecimal zeroAddMoney = com.multiply(zeroRat).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal firstAddMoney = com.multiply(fitstRat).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal addMoney = com.subtract(zeroAddMoney).subtract(firstAddMoney);
                expansionService.addExpansionCountMoney(addMoney.doubleValue(), expansion.getId());
                saveLogKd(orderCreditKd, expansion, null, addMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_SECOND, null);
                if(expansion.getToFirstRate() != 0){
                    Expansion parentExpansion = expansionService.getExpansionById(expansion.getParentId());
                    expansionService.addExpansionCountMoney(firstAddMoney.doubleValue(), expansion.getParentId());
                    saveLogKd(orderCreditKd, parentExpansion, expansion, firstAddMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_FIRST, null);
                }

                if(expansion.getToZeroRate() != 0){
                    staffService.addStaffAccountMoney(zeroAddMoney.doubleValue(), expansion.getStaffId());
                    staffService.addStaffCommissionMoney(zeroAddMoney.doubleValue(), expansion.getStaffId());
                    saveLogKd(orderCreditKd, null, expansion, zeroAddMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_ZERO, expansion.getStaffId());
                }

            } else if (expansion.getExpansionLevel() == CommonConstant.EXPANSION_LEVEL_FIRST) {
                BigDecimal com = new BigDecimal(orderCreditKd.getCommissionMoney());
                BigDecimal zeroRat = new BigDecimal(expansion.getToZeroRate());
                BigDecimal zeroAddMoney = com.multiply(zeroRat).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal addMoney = com.subtract(zeroAddMoney);
                expansionService.addExpansionCountMoney(addMoney.doubleValue(), expansion.getId());
                saveLogKd(orderCreditKd, expansion, null, addMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_FIRST, null);
                if(expansion.getToZeroRate() != 0){
                    staffService.addStaffAccountMoney(zeroAddMoney.doubleValue(), expansion.getStaffId());
                    staffService.addStaffCommissionMoney(zeroAddMoney.doubleValue(), expansion.getStaffId());
                    saveLogKd(orderCreditKd, null, expansion, zeroAddMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_ZERO, expansion.getStaffId());
                }

            }
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneyCb(OrderCreditMonth orderCreditMonth) {
        if (orderCreditMonth != null) {
            Expansion expansion = expansionService.getExpansionById(orderCreditMonth.getChannelId());
            //自有人员，2级给1级返佣 有发展奖
            if (expansion.getExpansionLevel() == CommonConstant.EXPANSION_LEVEL_SECOND) {
                BigDecimal com = new BigDecimal(orderCreditMonth.getCommissionMoney());
                BigDecimal fitstRat = new BigDecimal(expansion.getToFirstRate());
                BigDecimal zeroRat = new BigDecimal(expansion.getToZeroRate());
                BigDecimal zeroAddMoney = com.multiply(zeroRat).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal firstAddMoney = com.multiply(fitstRat).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal addMoney = com.subtract(zeroAddMoney).subtract(firstAddMoney);
                expansionService.addExpansionCountMoney(addMoney.doubleValue(), expansion.getId());
                saveLogMonth(orderCreditMonth, expansion, null, addMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_SECOND, null);
                if(expansion.getToFirstRate() != 0){
                    Expansion parentExpansion = expansionService.getExpansionById(expansion.getParentId());
                    expansionService.addExpansionCountMoney(firstAddMoney.doubleValue(), expansion.getParentId());
                    saveLogMonth(orderCreditMonth, parentExpansion, expansion, firstAddMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_FIRST, null);
                }

                if(expansion.getToZeroRate() != 0){
                    staffService.addStaffAccountMoney(zeroAddMoney.doubleValue(), expansion.getStaffId());
                    staffService.addStaffCommissionMoney(zeroAddMoney.doubleValue(), expansion.getStaffId());
                    saveLogMonth(orderCreditMonth, null, expansion, zeroAddMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_ZERO, expansion.getStaffId());
                }

            } else if (expansion.getExpansionLevel() == CommonConstant.EXPANSION_LEVEL_FIRST) {
                BigDecimal com = new BigDecimal(orderCreditMonth.getCommissionMoney());
                BigDecimal zeroRat = new BigDecimal(expansion.getToZeroRate());
                BigDecimal zeroAddMoney = com.multiply(zeroRat).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal addMoney = com.subtract(zeroAddMoney);
                expansionService.addExpansionCountMoney(addMoney.doubleValue(), expansion.getId());
                saveLogMonth(orderCreditMonth, expansion, null, addMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_FIRST, null);
                if(expansion.getToZeroRate() != 0){
                    staffService.addStaffAccountMoney(zeroAddMoney.doubleValue(), expansion.getStaffId());
                    staffService.addStaffCommissionMoney(zeroAddMoney.doubleValue(), expansion.getStaffId());
                    saveLogMonth(orderCreditMonth, null, expansion, zeroAddMoney.doubleValue(), CommonConstant.EXPANSION_LEVEL_ZERO, expansion.getStaffId());
                }

            }
        }
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneyQianY(OrderCreditCK orderCreditCK) {
        putoutCommissionMoneyCb(orderCreditCK);

    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneyQianY(OrderCreditKd orderCreditKd) {
        putoutCommissionMoneyCb(orderCreditKd);

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneyQianY(OrderCreditMonth orderCreditMonth) {
        putoutCommissionMoneyCb(orderCreditMonth);

    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneyQuanY(OrderCreditCK orderCreditCK) {
        if (orderCreditCK != null) {
            Expansion expansion = expansionService.getExpansionById(orderCreditCK.getChannelId());
            Staff staff = staffService.getStaffById(expansion.getStaffId());
            if (staff != null) {
                Mechanisms mechanisms = mechanismsService.selectMechanismsById(staff.getOrgId());
                if (mechanisms.getStaffState() == CommonConstant.STAFF_STATE_HAS) {
                    putoutCommissionMoneyZy(orderCreditCK);
                } else {
                    putoutCommissionMoneyCb(orderCreditCK);
                }
            } else {
                log.info("权益合伙人没发展人没有找到 合伙人id" + expansion.getId());
            }
        }

    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneyQuanY(OrderCreditKd orderCreditKd) {
        if (orderCreditKd != null) {
            Expansion expansion = expansionService.getExpansionById(orderCreditKd.getChannelId());
            Staff staff = staffService.getStaffById(expansion.getStaffId());
            if (staff != null) {
                Mechanisms mechanisms = mechanismsService.selectMechanismsById(staff.getOrgId());
                if (mechanisms.getStaffState() == CommonConstant.STAFF_STATE_HAS) {
                    putoutCommissionMoneyZy(orderCreditKd);
                } else {
                    putoutCommissionMoneyCb(orderCreditKd);
                }
            } else {
                log.info("权益合伙人没发展人没有找到 合伙人id" + expansion.getId());
            }
        }

    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void putoutCommissionMoneyQuanY(OrderCreditMonth orderCreditMonth) {
        if (orderCreditMonth != null) {
            Expansion expansion = expansionService.getExpansionById(orderCreditMonth.getChannelId());
            Staff staff = staffService.getStaffById(expansion.getStaffId());
            if (staff != null) {
                Mechanisms mechanisms = mechanismsService.selectMechanismsById(staff.getOrgId());
                if (mechanisms.getStaffState() == CommonConstant.STAFF_STATE_HAS) {
                    putoutCommissionMoneyZy(orderCreditMonth);
                } else {
                    putoutCommissionMoneyCb(orderCreditMonth);
                }
            } else {
                log.info("权益合伙人没发展人没有找到 合伙人id" + expansion.getId());
            }
        }

    }




    private void saveLogKd(OrderCreditKd orderCreditKd, Expansion expansion, Expansion returnExpansion, double finalCommissionMoney, int level, String staffId) {
        if (orderCreditKd != null) {
            AccountLog accountLog = new AccountLog();
            accountLog.setOrderNo(StringUtils.ramdomOrderNo());
            accountLog.setLevel(level);
            if (level == 0) {
                accountLog.setStaffId(staffId);
            } else {
                if (expansion != null) {
                    accountLog.setExpansionId(expansion.getId());
                    accountLog.setStaffId(expansion.getStaffId());
                    accountLog.setToZeroRate(expansion.getToZeroRate());
                    accountLog.setToFirstRate(expansion.getToFirstRate());
                } else {
                    log.info("不是0级 但是写日志是没有传渠道");
                }
            }
            accountLog.setAccountDate(new Date());
            accountLog.setAccountMoney(finalCommissionMoney);
            accountLog.setOrderCode(orderCreditKd.getOrderCode());

            if (returnExpansion != null) {
                accountLog.setReturnCommissionExpansionId(returnExpansion.getId());
            } else {
                accountLog.setFirstMoney(orderCreditKd.getFirstMoney());
                accountLog.setCommissionFirstMoneyUp(orderCreditKd.getCommissionFirstMoneyUp());
                accountLog.setCommissionFirstMoneyDown(orderCreditKd.getCommissionFirstMoneyDown());
                accountLog.setFirstCommissionMoney(orderCreditKd.getRuleCommissionMoney());
                if (expansion != null) {
                    accountLog.setToFirstRate(expansion.getToFirstRate());
                    accountLog.setToZeroRate(expansion.getToZeroRate());
                } else {
                    log.info("不是0级 但是写日志是没有传渠道");
                }
            }
            accountLog.setAccountState(CommonConstant.ACCOUNT_LOT_STATE_SUCCESS);
            accountLog.setAccountType(CommonConstant.ACCOUNT_LOT_TYPE_COMMISSION);
            accountLog.setIsContinueMoney(CommonConstant.IS_CONTINUE_MONEY_DC);
            accountLog.setBeginDate(orderCreditKd.getBeginDate());
            accountLog.setEndDate(orderCreditKd.getEndDate());
            accountLogService.insert(accountLog);
        }
    }


    private void saveLogCk(OrderCreditCK orderCreditCK, Expansion expansion, Expansion returnExpansion, double finalCommissionMoney, int level, String staffId) {
        if (orderCreditCK != null) {
            AccountLog accountLog = new AccountLog();
            accountLog.setOrderNo(StringUtils.ramdomOrderNo());
            accountLog.setLevel(level);
            if (level == 0) {
                accountLog.setStaffId(staffId);
            } else {
                if (expansion != null) {
                    accountLog.setExpansionId(expansion.getId());
                    accountLog.setStaffId(expansion.getStaffId());
                    accountLog.setToZeroRate(expansion.getToZeroRate());
                    accountLog.setToFirstRate(expansion.getToFirstRate());
                } else {
                    log.info("不是0级 但是写日志是没有传渠道");
                }
            }
            accountLog.setAccountDate(new Date());
            accountLog.setAccountMoney(finalCommissionMoney);
            accountLog.setOrderCode(orderCreditCK.getOrderCode());

            if (returnExpansion != null) {
                accountLog.setReturnCommissionExpansionId(returnExpansion.getId());
            } else {
                accountLog.setFirstMoney(orderCreditCK.getFirstMoney());
                accountLog.setCommissionFirstMoneyUp(orderCreditCK.getCommissionFirstMoneyUp());
                accountLog.setCommissionFirstMoneyDown(orderCreditCK.getCommissionFirstMoneyDown());
                accountLog.setFirstCommissionMoney(orderCreditCK.getRuleCommissionMoney());
                if (expansion != null) {
                    accountLog.setToFirstRate(expansion.getToFirstRate());
                    accountLog.setToZeroRate(expansion.getToZeroRate());
                } else {
                    log.info("不是0级 但是写日志是没有传渠道");
                }
            }
            accountLog.setAccountState(CommonConstant.ACCOUNT_LOT_STATE_SUCCESS);
            accountLog.setAccountType(CommonConstant.ACCOUNT_LOT_TYPE_COMMISSION);
            accountLog.setIsContinueMoney(CommonConstant.IS_CONTINUE_MONEY_DC);
            accountLog.setBeginDate(orderCreditCK.getBeginDate());
            accountLog.setEndDate(orderCreditCK.getEndDate());
            accountLogService.insert(accountLog);
        }
    }


    private void saveLogBf(OrderCreditBf orderCreditBf,String staffId) {
        if (orderCreditBf != null) {
            AccountLog accountLog = new AccountLog();
            accountLog.setOrderNo(StringUtils.ramdomOrderNo());
            accountLog.setLevel(orderCreditBf.getChannelLevel());
            accountLog.setStaffId(staffId);
            if (orderCreditBf.getChannelLevel() != 0) {
                accountLog.setExpansionId(orderCreditBf.getChannelId());
            }
            accountLog.setAccountDate(new Date());
            accountLog.setAccountMoney(orderCreditBf.getCommissionMoney());
            accountLog.setOrderCode(orderCreditBf.getOrderCode());
            accountLog.setAccountState(CommonConstant.ACCOUNT_LOT_STATE_SUCCESS);
            accountLog.setAccountType(CommonConstant.ACCOUNT_LOT_TYPE_COMMISSION);
            accountLog.setIsContinueMoney(CommonConstant.IS_CONTINUE_MONEY_LX);
            accountLogService.insert(accountLog);
        }
    }

    private void saveLogSuperadd(CommissionSuperadd commissionSuperadd,String staffId) {
        if (commissionSuperadd != null) {
            AccountLog accountLog = new AccountLog();
            accountLog.setOrderNo(StringUtils.ramdomOrderNo());
            accountLog.setLevel(commissionSuperadd.getChannelLevel());
            accountLog.setStaffId(staffId);
            if (commissionSuperadd.getChannelLevel() != 0) {
                accountLog.setExpansionId(commissionSuperadd.getChannelId());
            }
            accountLog.setAccountDate(new Date());
            accountLog.setAccountMoney(commissionSuperadd.getCommissionMoney());
            accountLog.setOrderCode(commissionSuperadd.getOrderCode());
            accountLog.setAccountState(CommonConstant.ACCOUNT_LOT_STATE_SUCCESS);
            accountLog.setAccountType(CommonConstant.ACCOUNT_LOT_TYPE_COMMISSION);
            accountLog.setIsContinueMoney(commissionSuperadd.getType());
            accountLogService.insert(accountLog);
        }
    }

    private void saveLogMonth(OrderCreditMonth orderCreditMonth, Expansion expansion, Expansion returnExpansion, double finalCommissionMoney, int level, String staffId) {
        if (orderCreditMonth != null) {
            AccountLog accountLog = new AccountLog();
            accountLog.setOrderNo(StringUtils.ramdomOrderNo());
            accountLog.setLevel(level);
            if (level == 0) {
                accountLog.setStaffId(staffId);
            } else {
                if (expansion != null) {
                    accountLog.setExpansionId(expansion.getId());
                    accountLog.setStaffId(expansion.getStaffId());
                    accountLog.setToZeroRate(expansion.getToZeroRate());
                    accountLog.setToFirstRate(expansion.getToFirstRate());
                } else {
                    log.info("不是0级 但是写日志是没有传渠道");
                }
            }
            accountLog.setAccountDate(new Date());
            accountLog.setAccountMoney(finalCommissionMoney);
            accountLog.setOrderCode(orderCreditMonth.getOrderCode());

            if (returnExpansion != null) {
                accountLog.setReturnCommissionExpansionId(returnExpansion.getId());
            } else {
                accountLog.setFirstMoney(orderCreditMonth.getFirstMoney());
                accountLog.setCommissionFirstMoneyUp(orderCreditMonth.getCommissionFirstMoneyUp());
                accountLog.setCommissionFirstMoneyDown(orderCreditMonth.getCommissionFirstMoneyDown());
                accountLog.setFirstCommissionMoney(orderCreditMonth.getRuleCommissionMoney().intValue());
                if (expansion != null) {
                    accountLog.setToFirstRate(expansion.getToFirstRate());
                    accountLog.setToZeroRate(expansion.getToZeroRate());
                } else {
                    log.info("不是0级 但是写日志是没有传渠道");
                }
            }
            accountLog.setAccountState(CommonConstant.ACCOUNT_LOT_STATE_SUCCESS);
            accountLog.setAccountType(CommonConstant.ACCOUNT_LOT_TYPE_COMMISSION);
            accountLog.setIsContinueMoney(CommonConstant.IS_CONTINUE_MONEY_AY);
            accountLog.setBeginDate(orderCreditMonth.getBeginDate());
            accountLog.setEndDate(orderCreditMonth.getEndDate());
            accountLogService.insert(accountLog);
            orderCreditMonthService.putOutOver(orderCreditMonth.getId(),new Date());
        }
    }
}
